﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;
public partial class Pub_Module_J_SearchProduct : System.Web.UI.UserControl
{

    protected void Page_Load(object sender, EventArgs e)
    {
        Ajax.Utility.RegisterTypeForAjax(typeof(Pub_Module_J_SearchProduct));
        if (!IsPostBack)
        {
            txtSearch.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + btnSearch.ClientID + "').click();return false;}} else {return true}; ");
            BindTopicList();
            BindCatalogueByTopicID();
            //FindValueTXT(txtSearch, Request.QueryString["PdName"]);
        }

    }


    private void BindTopicList()
    {
        clsWebShop cls = new clsWebShop();
        ListItem item = new ListItem();
        ddlTopic.DataSource = cls.Get_TopicNoneProc().Tables[0];
        ddlTopic.DataTextField = "TopicName";
        ddlTopic.DataValueField = "TopicID";
        ddlTopic.DataBind();
        item.Text = "-- Chọn chủ đề --";
        item.Value = "0";
        ddlTopic.Items.Insert(0, item);
        FindValueDLL(ddlTopic, Request.QueryString["TopicID"]);

    }



    private void BindCatalogueByTopicID()
    {
        string TopicID = ddlTopic.SelectedValue;
        if (Request.QueryString["TopicID"] != "" && Request.QueryString["TopicID"] != null)
            TopicID = Request.QueryString["TopicID"];


        ListItem item = new ListItem();
        DataTable dt = null;
        dt = AjaxGetcategoryID(TopicID).Tables[0];
        ddlCatalogue.DataSource = dt;
        ddlCatalogue.DataTextField = "Catname";
        ddlCatalogue.DataValueField = "CatID";
        ddlCatalogue.DataBind();
        item.Text = "-- Chọn thể loại--";
        item.Value = "0";
        ddlCatalogue.Items.Insert(0, item);
        FindValueDLL(ddlCatalogue, Request.QueryString["CatID"]);
    }

    private void FindValueDLL(DropDownList ddl, string value)
    {
        try
        {
            if (value != "" && value != null)
                ddl.Items.FindByValue(value).Selected = true;
        }
        catch (Exception ex) { }
    }

    private void FindValueTXT(TextBox txt, string value)
    {
        if (value != "" && value != null)
            txt.Text = value;
    }

    [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)]
    public DataSet AjaxGetcategoryID(string sIDTopic)
    {
        DataSet ds;
        clsWebShop cls = new clsWebShop();
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("CatName", "");
        msg.CreateFirstNode("TopicID", cls.killSQLInjection(sIDTopic));
        ds = cls.Get_CatalogueByCondition(msg.GetXmlStr.ToString());
        return ds;
    }
}
